/**
* This web service returns account records
* developed to test the account extract using 
* Apex web service
*
* Author - Sayeed Faruqi
* Client - Linksys (Cisco)
*
* Last Update - Nov, 2009
**/

global class AccountIntegration {

   // Global class for partner data
    global class accountData {
        webservice string id;
        webservice string name;
        webservice string website;
    }
   
      
    // Web Service method for returning partner data
    webservice static accountData getAccountData(String startDt, String endDt){
        accountData result;
        result = new accountData();
                    
            //Let's build the start and end date for query
            //paramters
            string stDt = startDt.substring(0,10);
            string stTm = startDt.substring(11,16);
            
            string edDt = endDt.substring(0,10);
            string edTm = endDt.substring(11,16);  
            
            // Let's convert the string into date and time parametrs
            // which we will use for SOQL queries to extract account records         
           datetime startDate = datetime.valueof(stDt + ' ' + stTm + ':00');   //seconds are added as 00
           datetime endDate   = datetime.valueof(edDt + ' ' + edTm + ':00');
           
           //Let's declare list of accounts
           list <account> ad = [select id, name, website
                                from account
                                where systemmodstamp >: startDate and systemmodstamp <:endDate
                                LIMIT 200];
            for (integer i =0; i <ad.size(); i++) {
               result.id = ad[i].id;
               result.name = ad[i].name;
               result.website = ad[i].website;
            system.debug('Selecting Account: '+ ad[i].name);   
           }
            system.debug ('Size of result -----> ' + result);
             return result;
        }
 
    // Web service for updating partner data
    // This web service will recieve an array of External Id 
    // and Names and will update Account Names as specified in array 
      
      webservice static string updateAccount (account acctIn ) {
      string returnFlag; 
      
      for (List <Account> accts :[Select id, name from account
                                            Where name = :acctIn.name]) {
                                update accts;
                                returnFlag = 'Success';
      }
        returnFlag = 'Success';
        return returnFlag;
        
    }
  
    //test method
    public static testMethod void testWebService(){
        
       // test get account method
        accountData testResult;
        testResult = accountIntegration.getAccountData('2007-01-01T00:00:00Z', '2010-01-01T00:00:00Z');
    
        // test updateAccount method
        account a = new account();
        a.name = 'Test Distributor Account';
        //a.  = '2345678-1';
        string result = accountIntegration.updateAccount(a);
        system.debug ('Returned Value from update account is : ' + result);
    }
}